﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Constant.Message;
using FinalPointSet.Model.Constants;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model.Knowledge;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using static FinalPointSet.Model.Manager.MessageManager;
namespace FinalPointSet.ViewModel.Page.Knowledge
{
    public partial class CustomParamSettingPageViewModel : ObservableObject
    {
        /// <summary>
        /// 自定义参数集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<CustomParam> _CustomParams = [];
        /// <summary>
        /// 当前选中的自定义参数
        /// </summary>
        [ObservableProperty]
        private CustomParam? _CustomParam;
        /// <summary>
        /// 显示/隐藏抽屉视图
        /// </summary>
        [ObservableProperty]
        private bool _IsDrawerOpen = false;
        /// <summary>
        /// 当前运行的参数
        /// </summary>
        [ObservableProperty]
        private CustomParam? _RunCustomParam = new();
        /// <summary>
        /// 当前运行的规则
        /// </summary>
        [ObservableProperty]
        private string _OutputResult = string.Empty;

        public CustomParamSettingPageViewModel()
        {
            CustomParams = new(AssetManager.Instance.GetJsonObjectList<CustomParam>().OrderBy(e => e.Sequence));
        }

        /// <summary>
        /// 打开抽屉视图
        /// </summary>
        [RelayCommand]
        private void OpenDrawer(CustomParam customParam)
        {
            Debug.Print($"CustomParamSettingPageViewModel.OpenDrawer(), mockRule={customParam?.Name}");
            if (customParam == null)
                return;

            OutputResult = string.Empty;
            RunCustomParam = CustomParam;
            IsDrawerOpen = true;
        }

        /// <summary>
        /// 运行规则
        /// </summary>
        [RelayCommand]
        private void Run()
        {
            Debug.Print($"CustomParamSettingPageViewModel.Run(), RunCustomParam={RunCustomParam?.Name}");
            if (RunCustomParam == null)
                return;

            OutputResult = string.Empty;

            try
            {
                //获取结果
                OutputResult = CustomParamHelper.Generate(RunCustomParam);
            }
            catch (Exception e)
            {
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Error, $"运行参数失败!, 失败原因:{e.Message}"));
            }
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        [RelayCommand]
        private void Add()
        {
            if (CustomParams == null)
                return;

            int maxID = CustomParams.Count > 0 ? CustomParams.Max(e => e.ID + 1) : 1001;
            int maxSeq = CustomParams.Count > 0 ? CustomParams.Max(e => e.Sequence + 1) : 1;
            CustomParam customParam = new()
            {
                ID = maxID,
                Name = $"参数{CustomParams.Count + 1}",
                Sequence = maxSeq,
                Script = "function Generate(args) {\r\n    const param = JSON.parse(args);\r\n    var currentTime = param.currentTime;\r\n    return currentTime;\r\n}",
            };
            CustomParams.Add(customParam);
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"新增自定义参数[{customParam.Name}]成功!"));
            Debug.Print($"CustomParamSettingPageViewModel.Add(), 新增自定义参数[{customParam.Name}]成功!");
        }

        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        private void Save()
        {
            if (CustomParams == null)
            {
                Debug.Print("CustomParamSettingPageViewModel.Save(),知识点数据为空");
                return;
            }
            AssetManager.Instance.UpdateJsonObjectList(CustomParams.ToList());
            AssetManager.Instance.SaveJsonObjectList<CustomParam>();
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"保存{CustomParams.Count}条数据成功!"));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="MockRule">自定义脚本</param>
        [RelayCommand]
        private void Delete(CustomParam customParam)
        {
            if (customParam == null || CustomParams == null)
                return;
            Debug.Print($"CustomParamSettingPageViewModel.Delete(), 删除自定义参数[{customParam.Name}]成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"删除规则[{customParam.Name}]成功!"));
            CustomParams.Remove(customParam);
        }
    }
}
